<template>
  <el-form :model="formRef" label-width="auto" >
      <template v-for="item in formConfig" :key="item.id">
        <el-form-item :label="item.label">
          <ElUpload v-model="form[item.id]"  @update:modelValue="url =>form[item.id] = url" v-if="item.type === 'upload'"/>
          <ElRadio v-model="form[item.id]" :options="item.options" v-else-if="item.type === 'radio'"/>
          <AddNursingProjects v-model="form[item.id]" v-else-if="item.type === 'AddNursingProjects'"></AddNursingProjects>
          <component
              v-else
              :is="`el-${item.type}`"
              v-bind="item.props"
              v-model="form[item.id]"
              :label="item.label"
          />
        </el-form-item>
      </template>
    <el-form-item >
      <div style="width: 100%;;display: flex;justify-content: center;">
        <el-button type="primary" @click="onSubmit(formRef)">提交</el-button>
        <el-button @click="emit('cancelForm')">取消</el-button>
      </div>
    </el-form-item>
  </el-form>
</template>

<script setup>
import {reactive, ref, useAttrs, watch} from 'vue'
import ElUpload from '@/components/ImageUpload.vue';
import ElRadio  from "@/components/SimpleForm/SimpleRadio.vue";
import AddNursingProjects from "@/components/AddNursingProjects.vue";
const form = defineModel({default:()=>ref({})})
const props = defineProps({
  formConfig:{
    type:Object,
    default:() => []
  },
  subMitApi:{
    type:Function,
    default:()=>{}
  }
});

const emit = defineEmits(['submit-success','cancelForm'])

const formRef = ref(null)

const onSubmit =async (ref) => {
  emit('submit-success',form.value)

  // TODO 暂不做校验
  return;
  if (!ref) return
  await ref.validate(async (valid, fields) => {
    if (valid) {
      debugger
    } else {
      console.log('error submit!', fields)
    }
  })
}

</script>